package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import java.util.Arrays;

import static com.shanxia.codec.Decoder.*;

/**
 * S -> C
 * 服务器下发获取充电桩日志信息命令
 */
@Data
public class Cmd1406 extends ProtocolDataBase {

    public static final int MIN_SIZE = 135; // V3暂时按1字节算

    private int v1;
    private int v2;
    private int logType;                    // 获取日志类型
    private String url;                     // FTP 服务器 IP 地址或者域名
    private String username;                // FTP 用户名
    private String password;                // FTP 密码
    private String logPath;                 // FTP 存放日志路径
    private int execWay;                    // 执行方式

    public Cmd1406(){
        super.setCmd(1406);
    }

    public static Cmd1406 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd1406 cmd = new Cmd1406();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        int offset = 0;
        cmd.v1 = getUint16(dataField, offset);
        offset += 2;
        cmd.v2 = getUint16(dataField, offset);
        offset += 2;
        // 1字节获取日志类型
        cmd.logType = getUint8(dataField[offset++]);
        // 32字节FTP服务器IP地址或者域名
        String url = getUint8(dataField[offset]) + "."
                + getUint8(dataField[offset + 1]) + "."
                + getUint8(dataField[offset + 2]) + "."
                + getUint8(dataField[offset + 3]);
        cmd.url = url;
        offset += 32;
        // 16字节ftp用户名
        cmd.username = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 16), true);
        offset += 16;
        // 16字节fpt密码
        cmd.password = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 16), true);
        offset += 16;
        // 32字节FTP 下载路径
        cmd.logPath = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 32), true);
        offset += 32;
        // 1字节执行方式
        cmd.execWay = getUint8(dataField[offset++]);
        return cmd;
    }

    @Override
    public byte[] getDataField() {
        byte[] data = new byte[MIN_SIZE];
        int offset = 0;
        getBytesAsUint16(data, offset, this.v1);
        offset += 2;
        getBytesAsUint16(data, offset, this.v2);
        offset += 2;
        // 1字节获取日志类型
        data[offset++] = (byte) logType;
        // 32字节FTP服务器IP地址或者域名
        String[] urls = url.split(".");
        data[offset] = (byte) Integer.parseInt(urls[0]);
        data[offset + 1] = (byte) Integer.parseInt(urls[1]);
        data[offset + 2] = (byte) Integer.parseInt(urls[2]);
        data[offset + 3] = (byte) Integer.parseInt(urls[3]);
        offset += 32;
        // 16字节FTP用户名
        encodeAscii(data, username, 16, offset);
        offset += 16;
        // 16字节FTP密码
        encodeAscii(data, password, 16, offset);
        offset += 16;
        // 32字节FTP存放日志路径
        encodeAscii(data, logPath, 32, offset);
        offset += 32;
        // 1字节执行方式
        data[offset] = (byte) execWay;
        return data;
    }
}
